package com.aigou.admin.controller;

import com.aigou.admin.annotation.ResponseResult;
import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.vo.ResponseVo;
import com.aigou.admin.entity.SearchHistory;
import com.aigou.admin.service.SearchHistoryService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author chai
 * @date 2024/12/8 0008
 * @Description
 */

@RestController
@RequestMapping("/search")
@AllArgsConstructor
@ResponseResult
public class SearchHistoryController{

    private final SearchHistoryService searchHistoryService;

    //将方法名称作为参数传递  m=save
    @RequestMapping("/save")
    public ResponseVo save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String searchwords = req.getParameter("search_words");
        Integer num = Integer.valueOf(req.getParameter("num"));
        String searchtime=req.getParameter("search_time");
        Boolean save = searchHistoryService.save(new SearchHistory(id,searchwords,num, LocalDateTime.now()));
        return ResponseVo.buildSuccess(save);
    }

    @RequestMapping("/update")
    public ResponseVo update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String searchwords = req.getParameter("search_words");
        Integer num = Integer.valueOf(req.getParameter("num"));
        Boolean update = searchHistoryService.update(new SearchHistory(id,searchwords,num, LocalDateTime.now()));
        return ResponseVo.buildSuccess(update);
    }

    @RequestMapping("/delete")
    public ResponseVo delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        boolean rs = searchHistoryService.remove(id);
        return ResponseVo.buildSuccess(rs);
    }

    @RequestMapping("/findById")
    public ResponseVo findById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        SearchHistory searchHistory = searchHistoryService.findById(id);
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(searchHistory);
    }

    @RequestMapping("/findAll")
    public ResponseVo findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //查询用户
        List<SearchHistory> searchHistories = searchHistoryService.findAll();
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(searchHistories);
    }

    @RequestMapping("/page")
    public ResponseVo page(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String current = req.getParameter("current");
        String pageSize = req.getParameter("pageSize");
        //查询用户
        PageEntity<SearchHistory> searchHistoryPageEntity = searchHistoryService.findSearchHistoryPage(Integer.parseInt(current), Integer.parseInt(pageSize));
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(searchHistoryPageEntity);
    }

    @RequestMapping("/pageByLike")
    public ResponseVo pageByLike(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String current = req.getParameter("current");
        String pageSize = req.getParameter("pageSize");
        String k=req.getParameter("k");
        //查询用户
        PageEntity<SearchHistory> searchHistoryPageEntity = searchHistoryService.findSearchHistoryLikePage(Integer.parseInt(current), Integer.parseInt(pageSize),k);
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(searchHistoryPageEntity);
    }

}
